package com.zrwl.poscloud.xo.mapper;

import com.zrwl.poscloud.base.entity.Message;
import com.zrwl.poscloud.base.superI.SuperMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @author 刘思童 AutoGenerate
 * @date 2021-04-20
 */
public interface MessageMapper extends SuperMapper<Message> {
    /**
     * 获取未过期的短信验证码
     *
     * @return
     */
    @Select("<script>" +
            "  select *" +
            "  from tb_message_sms" +
            "  where status = 2" +
            "  and model_type = 1" +
            "  and ADDDATE(send_time, INTERVAL 10 MINUTE) > sysdate()" +
            "group by mobile,uid " +
            "order by send_time desc" +
            "</script>"
    )
    List<Message> getUnexpiredSMSVerifyCode();
    /**
     * 获取未发送的短信
     *
     * @return
     */
    @Select("<script>" +
            "select * " +
            "from t_message_sms " +
            "where status = 1 " +
            "  and model_type = #{type} " +
            "<if test='type == 1'>" +
            "  and create_time &gt;= DATE_FORMAT(SYSDATE(), '%Y-%m-%d 00:00:00') " +
            "  and create_time &lt;= DATE_FORMAT(SYSDATE(), '%Y-%m-%d 23:59:59') " +
            "  and send_time is null " +
            "</if>" +
            "group by mobile,uid " +
            "order by create_time desc" +
            "</script>"
    )
    List<Message> getUnsentTextMessages(@Param("type") int type);

    /**
     * 获取未发送的短信验证码
     *
     * @param unexpiredSMSVerifyCode
     * @return
     */
    @Select("<script>" +
            "  select * from " +
            "t_message_sms where status = 1" +
            "        and model_type = 1" +
            " and  create_time &gt;= DATE_FORMAT(SYSDATE(),'%Y-%m-%d 00:00:00') and create_time &lt;=DATE_FORMAT(SYSDATE(),'%Y-%m-%d 23:59:59') " +
            "        <if test='unexpiredSMSVerifyCode != null and unexpiredSMSVerifyCode.size > 0'>" +
            "            and mobile not in " +
            "            <foreach item='item' index='index' collection='unexpiredSMSVerifyCode' open='(' separator=',' close=')'>" +
            "                #{item.mobile}" +
            "            </foreach>" +
            "        </if>" +
            " group by mobile,uid " +
            " order by create_time desc" +
            "</script>"
    )
    List<Message> getUnsentSMSVerifyCode(@Param("unexpiredSMSVerifyCode") List<Message> unexpiredSMSVerifyCode);

    /**
     * 修改短信状态
     *
     * @param status
     * @param uid
     * @return
     */
    @Update("<script>" +
            "update t_message_sms " +
            "set status = #{status}" +
            "<if test='sendTime != null'> " +
            ",send_time = #{sendTime} " +
            "</if>" +
            "where uid = #{uid}" +
            "</script>"
    )
    int updateSMSStatus(@Param("status") int status, @Param("uid") String uid, @Param("sendTime") Date sendTime);

    /**
     * 获取未发送微信消息
     *
     * @return
     */
    @Select("<script>" +
            "  select * from " +
            "t_message_wx where status = 1" +
            "        and template_id = #{templateId}" +
            " and  create_time &gt;= DATE_FORMAT(SYSDATE(),'%Y-%m-%d 00:00:00') and create_time &lt;=DATE_FORMAT(SYSDATE(),'%Y-%m-%d 07:59:59') " +
            " order by create_time desc" +
            "</script>")
    List<Message> getNotSentWechatMessage(String templateId);

    /**
     * 修改微信状态
     *
     * @param status
     * @param uid
     * @return
     */
    @Update("<script>" +
            "update t_message_wx " +
            "set status = #{status} " +
            "<if test='sendTime != null'> " +
            ",send_time = #{sendTime} " +
            "</if>" +
            "where uid = #{uid}" +
            "</script>"
    )
    int updateWXStatus(@Param("status") int status, @Param("uid") String uid, @Param("sendTime") Date sendTime);
}
